﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AB_SD_Entities;
using AB_SD_Commons.Helpers;
using System.Data.SqlClient;
using System.Data;

namespace AB_SD_DataAccess
{
    class ProgrammeDAO
    {
        /// <summary>
        /// Select a programme by a condition
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static Programme[] Select(string condition)
        {
            Programme[] result;

            DataTable ProgTable = SqlHelper.ExecuteSelectQuery("Select * program where " + condition, null);
            if (ProgTable != null)
            {
                result = new Programme[ProgTable.Rows.Count];
                for (int i = 0; i < ProgTable.Rows.Count; i++ )
                {
                    result[i].Id = ProgTable.Rows[i]["ProgrammeID"].ToString();
                    result[i].Name = ProgTable.Rows[i]["ProgrammeName"].ToString();
                    result[i].Descrip = ProgTable.Rows[i]["Description"].ToString();
                    result[i].ContactId = ProgTable.Rows[i]["ContactID"].ToString();
                    result[i].IsActive = Convert.ToBoolean(ProgTable.Rows[i]["IsActive"].ToString());
                }
                    

                return result;

            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// insert a new programme
        /// </summary>
        /// <param name="Prog"></param>
        /// <returns></returns>
        public static bool Insert(Programme Prog)
        {
             string strQuery = "insert into Programme(ProgrammeName,Description,ContactID,IsActive) values('@Name','@Descrip','@Contact',@IsActive)";
             SqlParameter[] sqlparams = new SqlParameter[4];
             sqlparams[0] = new SqlParameter("@Name", SqlDbType.VarChar);
             sqlparams[0].Value = Prog.Name;
             sqlparams[1] = new SqlParameter("@Descrip", SqlDbType.VarChar);
             sqlparams[1].Value = Prog.Descrip;
             sqlparams[2] = new SqlParameter("@Contact", SqlDbType.VarChar);
             sqlparams[2].Value = Prog.ContactId;
             sqlparams[3] = new SqlParameter("@IsActive", SqlDbType.Bit);
             sqlparams[3].Value = Prog.IsActive;

             return SqlHelper.ExecuteInsertQuery(strQuery, sqlparams);
        }
        /// <summary>
        /// update a programme
        /// </summary>
        /// <param name="Prog"></param>
        /// <returns></returns>
        public static bool Update(Programme Prog)
        {
            string strQuery = "update Programme set ProgrammeName='@Name' ,Description='@Descrip',ContactID='@Contact',IsActive=@IsActive where ProgrammeID='@ProgId'";
            SqlParameter[] sqlparams = new SqlParameter[4];
            sqlparams[0] = new SqlParameter("@Name", SqlDbType.VarChar);
            sqlparams[0].Value = Prog.Name;
            sqlparams[1] = new SqlParameter("@Descrip", SqlDbType.VarChar);
            sqlparams[1].Value = Prog.Descrip;
            sqlparams[2] = new SqlParameter("@Contact", SqlDbType.VarChar);
            sqlparams[2].Value = Prog.ContactId;
            sqlparams[3] = new SqlParameter("@IsActive", SqlDbType.Bit);
            sqlparams[3].Value = Prog.IsActive;
            sqlparams[4] = new SqlParameter("@ProgId", SqlDbType.Bit);
            sqlparams[4].Value = Prog.IsActive;
          
            return SqlHelper.ExecuteUpdateQuery(strQuery, sqlparams);
        }
    }
}
